<#assign title="">
<#assign description="">
<#assign copyright="">
<#assign company="">
<#assign author="">
<#assign since="">
package ${packageName};

/**
 * 
 * <p>Title: ${title}</p> 
 * 
 * <p>Description: ${description}</p> 
 * 
 * <p>Copyright: Copyright (c) ${copyright}</p> 
 * 
 * <p>Company: ${company}</p>
 * 
 * @author ${author}
 * @since  ${since}
 * @version 1.0
 */


import org.apache.log4j.Logger;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.sunwayworld.common.utils.QueryHibernateUtils;
import com.sunwayworld.common.utils.SplitPage;
import com.sunwayworld.common.utils.VCommList;
import com.sunwayworld.common.hibernate.HibernateSessionFactory;
import com.sunwayworld.common.utils.BeanUtils;

<#foreach package in packages?if_exists>
import ${package};
</#foreach>
public class ${className} implements ${interface.className}{

	private static Logger logger = Logger.getLogger(${className}.class.getName());

	/**
	 * Method create${valueObject.classNick} 新增 ${valueObject.classNick}
	 * 
	 * @param ${valueObject.className?uncap_first} ${valueObject.className}.class
	 * @return ${valueObject.className}
	 */
	public ${valueObject.className} create${valueObject.classNick}(${valueObject.className} ${valueObject.className?uncap_first}) {
		logger.debug("create${valueObject.classNick}(${valueObject.className} ${valueObject.className?uncap_first}) ");
		Transaction tx = null;
		Session session = HibernateSessionFactory.currentSession();
		
		try {
			tx = session.beginTransaction();
			session.save(${valueObject.className?uncap_first});
			tx.commit();
			return ${valueObject.className?uncap_first};
		} catch (Exception ex) {
			HibernateSessionFactory.rollbackTransaction(tx);
			logger.error(ex.getMessage());
			return null;
		} finally {
			HibernateSessionFactory.closeSession();
		}
		
	}


	/**
	 * Method delete${valueObject.classNick}s 删除 ${valueObject.classNick}
	 * 
	 * @param list List.class
	 */
	public void delete${valueObject.classNick}s(List list){
		logger.debug("delete${valueObject.classNick}s(List list)");
		Transaction tx = null;
		Session session = HibernateSessionFactory.currentSession();
		
		try {
			tx = session.beginTransaction();
			for (int i = 0; i < list.size(); i++) {
				${valueObject.className} ${valueObject.className?uncap_first} = (${valueObject.className}) list.get(i);
				${valueObject.className?uncap_first} = (${valueObject.className})session.get(${valueObject.className}.class, ${valueObject.className?uncap_first}.get${valueObject.pkField.name?cap_first}());
				session.delete(${valueObject.className?uncap_first});
			}
			logger.debug("delete " + list.size() + " ${valueObject.className}");
			tx.commit();
		} catch (Exception ex) {
			HibernateSessionFactory.rollbackTransaction(tx);
			logger.error(ex.getMessage());
		}finally {
			HibernateSessionFactory.closeSession();
		}
	}

	/**
	 * Method update${valueObject.classNick} 修改 ${valueObject.classNick}
	 * 
	 * @param ${valueObject.className?uncap_first} ${valueObject.className}.class
	 */
	public void update${valueObject.classNick}(${valueObject.className} ${valueObject.className?uncap_first}){
		logger.debug("update${valueObject.classNick}(${valueObject.className} ${valueObject.className?uncap_first})");
		Transaction tx = null;
		Session session = HibernateSessionFactory.currentSession();
		
		try {
			tx = session.beginTransaction();
			Object obj = session.get(${valueObject.className}.class, ${valueObject.className?uncap_first}.get${valueObject.pkField.name?cap_first}());
			BeanUtils.copyProperties(${valueObject.className?uncap_first}, obj);
			session.save(obj);
			tx.commit();
		} catch (Exception ex) {
			HibernateSessionFactory.rollbackTransaction(tx);
			logger.error(ex.getMessage());
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	/**
	 * Method get${valueObject.classNick}Detail 查询 ${valueObject.classNick} 详细信息
	 * 
	 * @param ${valueObject.pkField.name} ${valueObject.pkField.type}.class 主键
	 * @return ${valueObject.className}
	 */
	public ${valueObject.className} get${valueObject.classNick}Detail(${valueObject.pkField.type} ${valueObject.pkField.name}){
		logger.debug("get${valueObject.classNick}Detail(${valueObject.pkField.type} ${valueObject.pkField.name})");
		${valueObject.className} ${valueObject.className?uncap_first} = null;
		Transaction tx = null;
		Session session = HibernateSessionFactory.currentSession();
		
		try {
			tx = session.beginTransaction();
			${valueObject.className?uncap_first} = (${valueObject.className}) session.get(${valueObject.className}.class, ${valueObject.pkField.name});
			tx.commit();
		} catch (Exception ex) {
			logger.error(ex.getMessage());
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return ${valueObject.className?uncap_first};

	}

	/**
	 * Method get${valueObject.classNick}List 查询 List ${valueObject.className}.class
	 * 
	 * @param map Map.class 包含查询条件
	 * @param spage SplitPage.class 包含分页信息
	 * @return VCommList.class (${valueObject.className}.class)
	 */
	public VCommList get${valueObject.classNick}List(Map map,SplitPage spage){
		logger.debug("get${valueObject.classNick}List(Map map,SplitPage spage)");
		
		try {
			VCommList vlist = QueryHibernateUtils.getVCommList(${valueObject.className}.class,
					map, spage);
			return vlist;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			return null;
		}

	}

}